﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace auto_deduct_money
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();


            ConfigDic.getConfigTable();


            //清空日志  
            TimerClearException.Interval = 1000 * 60 * 60;      //1小时
            TimerClearException.Start();
            TimerClearException_Tick(null, null);

            //自动扣款
            TimerAuto.Interval = 1000 * 60 * 10;                //10分钟
            TimerAuto.Start();
            TimerAuto_Tick(null, null);

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void TimerClearException_Tick(object sender, EventArgs e)
        {
            //清空日志
            if (System.IO.File.Exists(Log.exceptionFileName) == true)
            {
                System.IO.FileInfo myExceptionLogInfo = new System.IO.FileInfo(Log.exceptionFileName);
                if (myExceptionLogInfo.Length > 100000)
                {
                    FileOperate.WriteFile(Log.exceptionFileName, "");
                }
            }
        }

        private void TimerAuto_Tick(object sender, EventArgs e)
        {
            //当天缴费的机构
            string sqlstr1 = "select * from t_cost_organization where autopay_type=0 and cast(datepart(dd,autopay_time) as int)=cast(datepart(dd,getdate()) as int)";
            DataTable dt1 = DataBaseOperate_SQLServer.GetDataTable(sqlstr1);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string sqpstr2 = "exec p_select_autobill " + dt1.Rows[i]["cost_organization_id"];
                DataTable dt2 = DataBaseOperate_SQLServer.GetDataTable(sqlstr1);

                for (int j = 0; j < dt2.Rows.Count; j++)
                {
                    if (dt2.Rows[j]["user_paid_id"] == DBNull.Value)
                    {
                        //未缴费则自动缴费
                        //调用接口到相应机构查询欠费情况


                        //调用存储过程p_insert_user_wait_pay（参数pay_time = dt1.Rows[i]["autopay_time"]）



                        //调用接口到相应机构缴费情况，并修改t_user_wait_pay表中的user_paid_id值（存储过程p_paid_atonce返回的表的第一行的第一个字段）。



                    }
                    else if (Convert.ToInt32(dt2.Rows[j]["user_paid_id"]) == 0)
                    {
                        //缴费未成功
                        //调用接口到相应机构缴费情况，并修改t_user_wait_pay表中的user_paid_id值（存储过程p_paid_atonce返回的表的第一行的第一个字段）。


                    }
                    else if (Convert.ToInt32(dt2.Rows[j]["user_paid_id"]) > 0)
                    {
                        //缴费成功

                        //无需操作
                    }

                }


            }
        }
    }
}
